﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:AllowancePayEntry
    /// </summary>
    public partial class AllowancePayEntry
    {
        public AllowancePayEntry()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fund.t_AllowancePayEntry");
            strSql.Append(" where FId=@FId");
            SqlParameter[] parameters = {
					new SqlParameter("@FId", SqlDbType.Decimal)
			};
            parameters[0].Value = FId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(EnterpriseFund.Model.AllowancePayEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into fund.t_AllowancePayEntry(");
            strSql.Append("FApplicationFormID,FPayCertNo,FPayDate,FAmount,FNote,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP)");
            strSql.Append(" values (");
            strSql.Append("@FApplicationFormID,@FPayCertNo,@FPayDate,@FAmount,@FNote,@FEntityNo,@FInsEmpNo,@FInsDeptNo,@FInsDate,@FInsIP,@FUpdEmplNo,@FUpdDeptNo,@FUpdDate,@FUpdIP)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FApplicationFormID", SqlDbType.NVarChar,50),
					new SqlParameter("@FPayCertNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPayDate", SqlDbType.DateTime),
					new SqlParameter("@FAmount", SqlDbType.Decimal,9),
					new SqlParameter("@FNote", SqlDbType.NVarChar,255),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15)};
            parameters[0].Value = model.FApplicationFormID;
            parameters[1].Value = model.FPayCertNo;
            parameters[2].Value = model.FPayDate;
            parameters[3].Value = model.FAmount;
            parameters[4].Value = model.FNote;
            parameters[5].Value = model.FEntityNo;
            parameters[6].Value = model.FInsEmpNo;
            parameters[7].Value = model.FInsDeptNo;
            parameters[8].Value = model.FInsDate;
            parameters[9].Value = model.FInsIP;
            parameters[10].Value = model.FUpdEmplNo;
            parameters[11].Value = model.FUpdDeptNo;
            parameters[12].Value = model.FUpdDate;
            parameters[13].Value = model.FUpdIP;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(EnterpriseFund.Model.AllowancePayEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fund.t_AllowancePayEntry set ");
            strSql.Append("FPayCertNo=@FPayCertNo,");
            strSql.Append("FPayDate=@FPayDate,");
            strSql.Append("FAmount=@FAmount,");
            strSql.Append("FNote=@FNote,");
            strSql.Append("FInsEmpNo=@FInsEmpNo,");
            strSql.Append("FInsDeptNo=@FInsDeptNo,");
            strSql.Append("FInsDate=@FInsDate,");
            strSql.Append("FInsIP=@FInsIP,");
            strSql.Append("FUpdEmplNo=@FUpdEmplNo,");
            strSql.Append("FUpdDeptNo=@FUpdDeptNo,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP");
            strSql.Append(" where FId=@FId");
            SqlParameter[] parameters = {
					new SqlParameter("@FPayCertNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPayDate", SqlDbType.DateTime),
					new SqlParameter("@FAmount", SqlDbType.Decimal,9),
					new SqlParameter("@FNote", SqlDbType.NVarChar,255),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@FId", SqlDbType.Decimal,13),
					new SqlParameter("@FApplicationFormID", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FPayCertNo;
            parameters[1].Value = model.FPayDate;
            parameters[2].Value = model.FAmount;
            parameters[3].Value = model.FNote;
            parameters[4].Value = model.FInsEmpNo;
            parameters[5].Value = model.FInsDeptNo;
            parameters[6].Value = model.FInsDate;
            parameters[7].Value = model.FInsIP;
            parameters[8].Value = model.FUpdEmplNo;
            parameters[9].Value = model.FUpdDeptNo;
            parameters[10].Value = model.FUpdDate;
            parameters[11].Value = model.FUpdIP;
            parameters[12].Value = model.FId;
            parameters[13].Value = model.FApplicationFormID;
            parameters[14].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fund.t_AllowancePayEntry ");
            strSql.Append(" where FId=@FId");
            SqlParameter[] parameters = {
					new SqlParameter("@FId", SqlDbType.Decimal)
			};
            parameters[0].Value = FId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fund.t_AllowancePayEntry ");
            strSql.Append(" where FId in (" + FIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.AllowancePayEntry GetModel(decimal FId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FId,FApplicationFormID,FPayCertNo,FPayDate,FAmount,FNote,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP from fund.t_AllowancePayEntry ");
            strSql.Append(" where FId=@FId");
            SqlParameter[] parameters = {
					new SqlParameter("@FId", SqlDbType.Decimal)
			};
            parameters[0].Value = FId;

            EnterpriseFund.Model.AllowancePayEntry model = new EnterpriseFund.Model.AllowancePayEntry();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.AllowancePayEntry DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.AllowancePayEntry model = new EnterpriseFund.Model.AllowancePayEntry();
            if (row != null)
            {
                if (row["FId"] != null && row["FId"].ToString() != "")
                {
                    model.FId = decimal.Parse(row["FId"].ToString());
                }
                if (row["FApplicationFormID"] != null)
                {
                    model.FApplicationFormID = row["FApplicationFormID"].ToString();
                }
                if (row["FPayCertNo"] != null)
                {
                    model.FPayCertNo = row["FPayCertNo"].ToString();
                }
                if (row["FPayDate"] != null && row["FPayDate"].ToString() != "")
                {
                    model.FPayDate = DateTime.Parse(row["FPayDate"].ToString());
                }
                if (row["FAmount"] != null && row["FAmount"].ToString() != "")
                {
                    model.FAmount = decimal.Parse(row["FAmount"].ToString());
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FInsDeptNo"] != null)
                {
                    model.FInsDeptNo = row["FInsDeptNo"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FUpdDeptNo"] != null)
                {
                    model.FUpdDeptNo = row["FUpdDeptNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FEmpName"] != null)
                {
                    model.FInsEmpName = row["FEmpName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  select FId,FApplicationFormID,FPayCertNo,FPayDate,FAmount,FNote,fund.t_AllowancePayEntry.FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP,FEmpName
         FROM fund.t_AllowancePayEntry inner join t_HREmpl on t_HREmpl.FEntityNo=fund.t_AllowancePayEntry.FEntityNo and FInsEmpNo=FEmplNo ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FId,FApplicationFormID,FPayCertNo,FPayDate,FAmount,FNote,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP ");
            strSql.Append(" FROM fund.t_AllowancePayEntry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fund.t_AllowancePayEntry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FId desc");
            }
            strSql.Append(")AS Row, T.*  from fund.t_AllowancePayEntry T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_AllowancePayEntry";
            parameters[1].Value = "FId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

